package com.example.xadmin.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.xadmin.pojo.SysPermission;

import java.util.List;

public class TreeUtil {

    public static void tree(Integer parentId, List<SysPermission> list,JSONArray jsonArray){
        for (SysPermission item : list) {
            if(item.getParentId().equals(parentId)){
             String s = JSONObject.toJSONString(item);
             JSONObject parent = (JSONObject)JSONObject.parseObject(s);
             jsonArray.add(parent);
             if(list.stream().filter(p->p.getParentId().equals(item.getId())).findAny()!=null){
                 JSONArray child = new JSONArray();
                 parent.put("child",child);
                 tree(item.getId(),list,child);
             }
            }
        }
    }

}
